﻿<?php
/*
 * 用户中心
 * 1、得到openid
 * 2、通过openid检索用户
 * 3、用户存在，跳转到用户中心
 * 4、用户不存在，跳转到注册
 */
require_once '../init.php';
define('TEMPLATE_PATH', WANYA_ROOT . '/m/view/');

$appid = "wx74e5910c0ad82aa1";
$secret = "07ddbe5ddfc5679a33a77b4969ad9c33";

$code = $_GET["code"];

// 得到token
/*$get_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code';
$res=file_get_contents($get_token_url);

$token_obj = json_decode($res,true);
$openid = $token_obj['openid'];
$access_token = $token_obj['access_token'];*/
$openid="ojl7ct47Th1rtOupo3VtS6CX6IGM";
$User_Info_Model = new User_Info_Model();

$users = array_filter($User_Info_Model->getUserInfo($openid));



if (empty($users)) {
    $get_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN';
    $res=file_get_contents($get_info_url);
    $info_obj = json_decode($res,true);

    $site_title = "成为会员";
    // TODO　获得用户基本信息后，调转注册页面
    include View::getView('user/userReg');
    View::output();
} else if ($User_Info_Model->getUserInfoByFamilyId($users["family_id"])== 1) {
    $userData = array();
    $userData["openid"] = $openid;
    include View::getView('user/addChild');
    View::output();
} else {

    $site_title = "个人中心 ";
    $userInfo = $User_Info_Model->getUserInfo($openid);

    $userInfo = $User_Info_Model->getUserInfoForView($openid);

    $User_Master_Model = new User_Master_Model();
    // 积分 作品热度
    $userMaster = $User_Master_Model->getUserMaster($userInfo["family_id"]);
    // 热度排名
    $hotIndex = $User_Master_Model->getHotIndex($userMaster['hot']);

    if($hotIndex==0)
    {$hotIndex=1;}
    $Productions_Model = new Productions_Model();

    // 上传作品数
    $productionSum = $Productions_Model->getProductionSumByOpenid($openid);

    // 该家族下所有的用户的 昵称.
    $userSimpleInfos = $User_Info_Model->getSimpleUserInfo($userInfo["family_id"]);

    include View::getView('user/userCenter');
    View::output();
}
?>
